package com.xinxin.blockchain.service.impl;

import com.xinxin.base.service.IRedisService;
import com.xinxin.blockchain.constant.RedisKeyConstant;
import com.xinxin.blockchain.dao.impl.SystemConfigDao;
import com.xinxin.blockchain.enums.ConfigTypeEnum;
import com.xinxin.blockchain.model.SystemConfig;
import com.xinxin.blockchain.service.ISystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ys
 * @date 2021/6/8 0008
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SystemConfigServiceImpl extends SystemConfigDao implements ISystemConfigService {

    @Autowired
    private IRedisService redisService;

    @Override
    public SystemConfig getConfig(ConfigTypeEnum type, String key) {
        return super.getConfig(type.getType(), key);
    }

    @Override
    public SystemConfig getConfigCacheable(ConfigTypeEnum type, String key) {
        String cacheKey = RedisKeyConstant.SYSTEM_CONFIG + type.name() + ":" + key;
        return redisService.orElseGet(cacheKey, SystemConfig.class, () -> getConfig(type, key));
    }

    @Override
    public List<SystemConfig> listConfig(ConfigTypeEnum type, String key) {
        return super.listConfig(type.getType(), key);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<SystemConfig> listConfigCacheable(ConfigTypeEnum type, String key) {
        String cacheKey = RedisKeyConstant.SYSTEM_CONFIG + type.name() + ":" + key;
        return redisService.orElseGet(cacheKey, List.class, () -> listConfig(type, key));
    }
}
